﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataWorks_Tools.ProcessExcute.ProcessReporter
{
    public static class ProgressReporter
    {
        private static ConcurrentDictionary<string, (int Percent, string Message, bool Finished)> _progress
            = new ConcurrentDictionary<string, (int, string, bool)>();

        public static void Report(string taskId, int percent, string message, bool finished = false)
        {
            _progress[taskId] = (percent, message, finished);

            if (finished)
            {
                // 自动延迟清理
                _ = Task.Run(async () =>
                {
                    await Task.Delay(TimeSpan.FromMinutes(1));
                    Clear(taskId);
                });
            }
        }

        public static (int Percent, string Message, bool Finished)? GetProgress(string taskId)
        {
            if (_progress.TryGetValue(taskId, out var value))
                return value;
            return null;
        }

        private static void Clear(string taskId)
        {
            _progress.TryRemove(taskId, out _);
        }
    }
}
